package com.jic.product.controller.manage;


import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.product.common.util.BeanUtil;
import com.jic.product.entity.ProductUnits;
import com.jic.product.manage.request.ProductUnitsEditRequest;
import com.jic.product.manage.request.ProductUnitsRequest;
import com.jic.product.manage.response.ProductUnitsResponse;
import com.jic.product.service.ProductUnitsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by zxk on 2020/1/9 11:04
 * 商品计量单位管理
 */
@RestController
@RequestMapping("/productUnits")
@Api(value = "商品设置", description = "计量单位管理", tags = "商品管理")
public class ProductUnitsController {

    @Autowired
    private ProductUnitsService productUnitsService;

    /*
     * 分页查询
     * */
    @ApiOperation(value = "获取所有商品计量单位信息")
    @ResponseBody
    @PostMapping(value = "/queryPage")
    public RestResult<PageResult<ProductUnitsResponse>> queryPage(@RequestBody ProductUnitsRequest form) {
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        ProductUnits productUnits = BeanUtil.copy(form, ProductUnits.class);
        PageResult<ProductUnits> result = productUnitsService.queryPage(productUnits, page);
        List<ProductUnitsResponse> productUnitsResponseList = result.getRows().stream().map(temp -> {
            ProductUnitsResponse productUnitsResponse = BeanUtil.copy(temp, ProductUnitsResponse.class);
            return productUnitsResponse;
        }).filter(item -> item != null).collect(Collectors.toList());

        PageResult<ProductUnitsResponse> pageResult = new PageResult<>();
        pageResult.setRows(productUnitsResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);

    }

    @ApiOperation(value = "获取所有计量单位")
    @PostMapping("/queryProductUnits")
    @ResponseBody
    public RestResult<List<ProductUnitsResponse>> queryProductUnits(@RequestBody ProductUnitsRequest productUnitsRequest) {
        List<ProductUnitsResponse> result = productUnitsService.query(productUnitsRequest);
        return RestResult.success(result);
    }

    /*
     *  商品计量单位新增
     *
     * */
    @ResponseBody
    @PostMapping(value = "/editProductUnits")
    public RestResult insertProductUnits(@RequestBody ProductUnitsEditRequest productUnitsEditRequest) {
        RestResult restResult = new RestResult<>();
        try {
            //修改
            if (productUnitsEditRequest.getId() != 0) {
                productUnitsEditRequest.setUpdater(productUnitsEditRequest.getAdminOperate().getOperator());
                productUnitsService.update(productUnitsEditRequest);
                restResult = RestResult.success(restResult);
            } else {//新增
                productUnitsEditRequest.setDeptId(productUnitsEditRequest.getAdminOperate().getDeptId());
                productUnitsEditRequest.setCreator(productUnitsEditRequest.getAdminOperate().getOperator());
                productUnitsService.insert(productUnitsEditRequest);
                restResult = RestResult.success(restResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
            restResult = RestResult.error("9999", e.getLocalizedMessage());
        }
        return restResult;

    }



    /*
     *  商品计量单位删除
     *
     * */
    @ResponseBody
    @PostMapping(value = "/deleteProductUnits")
    public RestResult deleteProductUnits(Long id) {
        RestResult restResult = new RestResult<>();
        try {
            long flag = productUnitsService.delete(id);
            if (flag == 1) {
                restResult = RestResult.success(restResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
            restResult = RestResult.error("9999", e.getLocalizedMessage());
        }
        return restResult;

    }
}
